package org.wangblue.document;

import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.wangblue.common.Client;

import java.io.IOException;
import java.util.Arrays;

/**
 * @Description: 批量删除文档
 * @Author: WangBlue
 * @Create: 2024-10-24 10:35
 * @Version: 1.0
 **/

public class BatchDeleteDocument {
    private static final String INDEX_NAME = "user";
    public static void main(String[] args) throws IOException {
        try (RestHighLevelClient client = Client.getClient()) {
            // 1. 批量删除文档
            BulkRequest request = new BulkRequest();
            request.add(new DeleteRequest(INDEX_NAME, "1001"));
            request.add(new DeleteRequest(INDEX_NAME, "1002"));
            request.add(new DeleteRequest(INDEX_NAME, "1003"));

            // 2. 客户端发送请求， 获取响应对象
            BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);

            //打印结果信息
            System.out.println("took:" + responses.getTook());
            System.out.println("items:" + Arrays.toString(responses.getItems()));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
